package com.skyline.user.dao.impl;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Repository;

import com.skyline.base.dao.impl.BaseDaoImpl;
import com.skyline.user.dao.PersonalInfoDao;
import com.skyline.user.mapper.PersonalIfonMapper;
import com.skyline.user.model.User;
import com.skyline.user.type.Sex;

//TODO:加注释
@Repository("personalInfoDao")
public class PersonalInfoDaoImpl extends BaseDaoImpl implements PersonalInfoDao {
	/**
	 * update user set name=?, nickName=?, birthday=?, sex=?, province=?,
	 * city=?, country=?, job=?, graduateSchool=? where userid=?
	 * */
	@Value("${PersonalInfoDao.updateBasicInfo}")
	private String updateBasicInfoSql;

	/**
	 * update user set mobilephone=?, msn=?, qq=? where userid=?
	 * */
	@Value("${PersonalInfoDao.updateCommunicationInfo}")
	private String updateCommunicationInfoSql;

	/**
	 * select email,name,nickName,birthday,sex,province,city,country,siteID,job,
	 * graduateSchool,mobilephone,msn,qq from user where userid=?
	 * */
	@Value("${PersonalInfoDao.queryPersonInfoByUserID}")
	private String queryPersonInfoByUserIDSql;

	/**
	 * select email,name,nickName,birthday,sex,province,city,country,siteID,job,
	 * graduateSchool,mobilephone,msn,qq from user where email=?
	 * */
	@Value("${PersonalInfoDao.queryPersonInfoByEmail}")
	private String queryPersonInfoByEmailSql;
	/**
	 * select
	 * id,email,name,nickname,portrait,birthday,sex,province,city,country,
	 * siteId,job,graduateSchool,mobilephone,msn,qq from user where id=? and
	 * password=?
	 */
	@Value("${PersonalInfoDao.queryPersonInfoByIdAndPassword}")
	private String queryPersonInfoByIdAndPasswordSql;
	/**
	 * update user set password=? where id=?
	 */
	@Value("${PeronnalInfoDao.updatePasswordInfo}")
	private String updatePasswordInfoSql;
	
	/**
	 * update user set portrait=? where id=?
	 */
	@Value("${PeronnalInfoDao.updateUserPortrait}")
	private String updateUserPortraitSql;

	@Override
	public void updateBasicInfo(Long userid, String name, String nickName,
			Date birthday, Sex sex, String province, String city,
			String country, String siteId, String job, String graduateSchool) {
		jdbcTemplate.update(updateBasicInfoSql, new Object[] { name, nickName,
				birthday, sex.toString(), province, city, country, siteId, job,
				graduateSchool, userid });
	}

	@Override
	public void updateCommunicationInfo(Long userid, String mobilephone,
			String msn, String qq) {
		jdbcTemplate.update(updateCommunicationInfoSql, new Object[] {
				mobilephone, msn, qq, userid });
	}

	@Override
	public User queryPersonInfoByUserID(Long userid) {
		List<User> users = jdbcTemplate.query(queryPersonInfoByUserIDSql,
				PersonalIfonMapper.getMapper(), userid);
		if (users.size() > 0)
			return users.get(0);
		else
			return null;
	}

	@Override
	public User queryPersonInfoByEmail(String email) {
		List<User> users = jdbcTemplate.query(queryPersonInfoByEmailSql,
				PersonalIfonMapper.getMapper(), email);
		if (users.size() > 0)
			return users.get(0);
		else
			return null;
	}

	@Override
	public User queryPersonInfoByIdAndPassword(Long userId, String password) {
		List<User> users = jdbcTemplate.query(
				queryPersonInfoByIdAndPasswordSql,
				PersonalIfonMapper.getMapper(), userId, password);
		if (users.size() > 0)
			return users.get(0);
		else
			return null;
	}

	@Override
	public void updatePasswordInfo(Long userId, String password) {
		jdbcTemplate.update(updatePasswordInfoSql, password, userId);

	}

	@Override
	public void updateUserPortrait(Long userId, String portrait) {
		jdbcTemplate.update(updateUserPortraitSql, portrait, userId);
		
	}

}
